package com.bafa.objects.player;

import java.util.TreeMap;

import com.bafa.services.RetrieveServices;

public class Player {
    
	private TreeMap<Integer,PlayerMatch> matchGoals= new TreeMap();
    private TreeMap<Integer,PlayerDivision> divisionGoals= new TreeMap();
    private String firstname;
    private int id;
    private TreeMap<Integer,PlayerSeason> seasonGoals = new TreeMap();
    private int seasonId;
    private String surname;
    private TreeMap<Integer,PlayerTeam> teamGoals= new TreeMap();
    private int teamId;
    private int totalGoals;
    public Player() {}
    public Player(String firstname,String surname, int teamId, int seasonId, int id,int goals){
        this.firstname=firstname;
        this.surname=surname;
        this.teamId=teamId;
        this.seasonId=seasonId;
        this.id=id;
        this.totalGoals=goals;
    }
	public void enterMatchGoals(int matchId, int goals){
		PlayerMatch match = new PlayerMatch();
		match.setGoals(goals);
		match.setMatch(matchId);
		matchGoals.put(matchId,match);
	}
	public void enterGoals(int goals, int seasonId, int division, int teamId){
		boolean isLeague = (RetrieveServices.getSeason(""+seasonId).isLeague());
        //Division goals
        if(isLeague){
		if(divisionGoals.containsKey(division)){
            ((PlayerDivision)divisionGoals.get(division)).setGoals(((PlayerDivision)divisionGoals.get(division)).getGoals()+goals);
        }else{
            PlayerDivision playerDivision = new PlayerDivision();
            playerDivision.setDivision(division);
            playerDivision.setGoals(goals);
            divisionGoals.put(division,playerDivision);
        }
        }
        // Team Goals
        if(teamGoals.containsKey(teamId)){
            ((PlayerTeam)teamGoals.get(teamId)).setGoals(((PlayerTeam)teamGoals.get(teamId)).getGoals()+goals);
        }else{
            PlayerTeam playerTeam = new PlayerTeam();
            playerTeam.setTeamId(teamId);
            playerTeam.setGoals(goals);
            teamGoals.put(teamId,playerTeam);
        }
        
        // Season Goals
        if(seasonGoals.containsKey(seasonId)){
			PlayerSeason playerSeason = (PlayerSeason)seasonGoals.get(seasonId);
            playerSeason.setGoals(((PlayerSeason)seasonGoals.get(seasonId)).getGoals()+goals);
			PlayerDivision playerDivision = new PlayerDivision();
			PlayerTeam playerTeam = new PlayerTeam();
			PlayerMatch playerMatch = new PlayerMatch();
			if(playerSeason.getDivisionGoals().containsKey(division)){
				playerDivision = (PlayerDivision)playerSeason.getDivisionGoals().get(division);
			}
			if(playerSeason.getTeamGoals().containsKey(teamId)){
				playerTeam = (PlayerTeam)playerSeason.getTeamGoals().get(teamId);
			}
			
			playerTeam.setTeamId(teamId);
			playerTeam.setGoals(playerTeam.getGoals()+goals);
			playerSeason.addTeamGoals(teamId,playerTeam);
			playerDivision.setDivision(division);
			playerDivision.setGoals(playerDivision.getGoals()+goals);
			playerSeason.addDivisionGoals(division,playerDivision);
			
        }else{
            PlayerSeason playerSeason = new PlayerSeason();
			PlayerDivision playerDivision = new PlayerDivision();
			PlayerTeam playerTeam = new PlayerTeam();
			if(playerSeason.getDivisionGoals().containsKey(division)){
				playerDivision = (PlayerDivision)playerSeason.getDivisionGoals().get(division);
			}
			if(playerSeason.getTeamGoals().containsKey(teamId)){
				playerTeam = (PlayerTeam)playerSeason.getTeamGoals().get(teamId);
			}
			
			playerDivision.setDivision(division);
			playerDivision.setGoals(goals);
			playerTeam.setTeamId(teamId);
			playerTeam.setGoals(goals);
            playerSeason.setSeasonId(seasonId);
            playerSeason.setGoals(goals);
			playerSeason.getDivisionGoals().put(division,playerDivision);
			playerSeason.addTeamGoals(teamId,playerTeam);
            seasonGoals.put(Integer.valueOf(seasonId),playerSeason);
        }
        
        totalGoals+=goals;
        
    }

	public void addDivisionGoals(int id, PlayerDivision division){
		divisionGoals.put(id,division);
	}
	public void addSeasonGoals(int id, PlayerSeason season){
		seasonGoals.put(id,season);
	}
	public void addTeamGoals(int id, PlayerTeam team){
		teamGoals.put(id,team);
	}
    public TreeMap getDivisionGoals() {
        return divisionGoals;
    }

    public String getFirstname() {
        return firstname;
    }

    public int getId() {
        return id;
    }

    public TreeMap getSeasonGoals() {
        return seasonGoals;
    }

    public int getSeasonId() {
        return seasonId;
    }

    public String getSurname() {
        return surname;
    }

    public TreeMap getTeamGoals() {
        return teamGoals;
    }

    public int getTeamId() {
        return teamId;
    }

    public int getTotalGoals() {
        return totalGoals;
    }


    public void setDivisionGoals(TreeMap divisionGoals) {
        this.divisionGoals = divisionGoals;
    }

    
	public void addDivisionGoal(int division,PlayerDivision divisionGoal){
		divisionGoals.put(division,divisionGoal);
	}
	public void addTeamGoal(int team,PlayerTeam divisionGoal){
		teamGoals.put(team,divisionGoal);
	}
	public void addSeasonGoal(int season,PlayerSeason seasonGoal){
		seasonGoals.put(season,seasonGoal);
	}
	public void addMatchGoals(int season,PlayerMatch seasonGoal){
		matchGoals.put(season,seasonGoal);
	}
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setSeasonGoals(TreeMap seasonGoals) {
        this.seasonGoals = seasonGoals;
    }

    public void setSeasonId(int seasonId) {
        this.seasonId = seasonId;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public void setTeamGoals(TreeMap teamGoals) {
        this.teamGoals = teamGoals;
    }

    public void setTeamId(int teamId) {
        this.teamId = teamId;
    }
    
    public void setTotalGoals(int goals) {
        this.totalGoals = goals;
    }
	public TreeMap<Integer, PlayerMatch> getMatchGoals() {
		return matchGoals;
	}
}
